package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author fenglin
 * @date 2019/12/7 19:38
 */
@Service
public class CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    public List<CategoryDTO> queryCategoryByParent(Long pid) {
        //创建对象，分装查询条件，对象中的非空字段会成为查询的条件
        Category category = new Category();
        category.setParentId(pid);
        //查询
        List<Category> list = categoryMapper.select(category);
        //非空校验,查询失败返回404
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //PO转换为DTO
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }

    public List<CategoryDTO> queryCategoryByIds(List<Long> categoryIds) {
        List<Category> list = categoryMapper.selectByIdList(categoryIds);
        //非空校验,查询失败返回404
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //PO转换为DTO
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }
}
